﻿/*
 * modbustcpmaster.hpp
 *
 *  Created on: 2017年10月29日
 *      Author: work
 */

#ifndef _DM_PROTOCOL_MODBUSTCPMASTER_HPP_
#define _DM_PROTOCOL_MODBUSTCPMASTER_HPP_

#include <dm/export.hpp>

#ifndef DM_API_PROTOCOL
#define DM_API_PROTOCOL DM_API_IMPORT
#endif

#include <dm/protocol/modbustcp.hpp>

namespace dm{
namespace protocol{

class DM_API_PROTOCOL CModbusTcpMaster:virtual public CModbusTcp{
public:
	CModbusTcpMaster();
	virtual ~CModbusTcpMaster();

	inline const dm::uint16& getUnitFlag()const{
		return m_unitFlag;
	}

	inline void setUnitFlag( const dm::uint8& unit ){
		m_unitFlag = unit;
	}

	virtual void reset( const ts_t& ts,const rts_t& rts );

	EAction dealRxFrame( const ts_t& ts,const rts_t& rts );

protected:
	CFrameModbusTcp* genRxTempFrame();
	CFrameModbusTcp* genTxTempFrame();

	virtual EAction taskStart_call( CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction taskDo_call( CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );

	virtual EAction taskStart_syncClock( CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction taskDo_syncClock( CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );

	virtual EAction taskStart_remoteControl( CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction taskDo_remoteControl( CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );

	virtual EAction taskStart_parameters( CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction taskDo_parameters( CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );

	virtual EAction dealRxResponse_readCoils( const CFrameModbusTcp& rf,CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction dealRxResponse_readDiscreteInputs( const CFrameModbusTcp& rf,CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction dealRxResponse_readHoldingRegisters( const CFrameModbusTcp& rf,CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction dealRxResponse_readInputRegisters( const CFrameModbusTcp& rf,CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );

	virtual EAction dealRxResponse_writeSingleCoil( const CFrameModbusTcp& rf,CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction dealRxResponse_writeSingleRegister( const CFrameModbusTcp& rf,CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );

	virtual EAction dealRxResponse_writeMultipleCoils( const CFrameModbusTcp& rf,CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction dealRxResponse_writeMultipleRegisters( const CFrameModbusTcp& rf,CFrameModbusTcp& tf,const ts_t& ts,const rts_t& rts );

	virtual void coils_01_error( const dm::uint16& address,const CFrameModbusTcp::EErrCode& ec,const ts_t& ts,const rts_t& rts );
	virtual void coils_01( const dm::uint16& address,const dm::uint8& value,const ts_t& ts,const rts_t& rts );

	virtual void discreteInputs_02_error( const dm::uint16& address,const CFrameModbusTcp::EErrCode& ec,const ts_t& ts,const rts_t& rts );
	virtual void discreteInputs_02( const dm::uint16& address,const dm::uint8& value,const ts_t& ts,const rts_t& rts );

	virtual void holdingRegisters_03_error( const dm::uint16& address,const CFrameModbusTcp::EErrCode& ec,const ts_t& ts,const rts_t& rts );
	virtual void holdingRegisters_03( const dm::uint16& address,const dm::uint16& value,const ts_t& ts,const rts_t& rts );

	virtual void inputRegisters_04_error( const dm::uint16& address,const CFrameModbusTcp::EErrCode& ec,const ts_t& ts,const rts_t& rts );
	virtual void inputRegisters_04( const dm::uint16& address,const dm::uint16& value,const ts_t& ts,const rts_t& rts );


	EAction taskStart_call( const ts_t& ts,const rts_t& rts );

	EAction taskDo_call( const ts_t& ts,const rts_t& rts );

	EAction taskStart_syncClock( const ts_t& ts,const rts_t& rts );

	EAction taskDo_syncClock( const ts_t& ts,const rts_t& rts );

	EAction taskStart_remoteControl( const ts_t& ts,const rts_t& rts );
	EAction taskDo_remoteControl( const ts_t& ts,const rts_t& rts );


	EAction taskStart_parameters( const ts_t& ts,const rts_t& rts );
	EAction taskDo_parameters( const ts_t& ts,const rts_t& rts );

	EAction setFrameTransflag( const EAction& act,CFrameModbusTcp& tf );
protected:
	dm::uint16 m_transFlag;
	dm::uint16 m_unitFlag;
};

}
}

#endif /* DM_RUNTIME_INCLUDE_DM_PROTOCOL_MODBUSTCPMASTER_HPP_ */
